﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using System.Text.RegularExpressions;
using WatiN.Core;
using DotNetNuke.Tests.UI.WatiN.Common;

namespace DotNetNuke.Tests.UI.WatiN.Common.WatiNObjects
{
    public class TelerikEditor : ModulePage
    {
        #region Constructors

        public TelerikEditor(WatiNBase watinBase) : base(watinBase.IEInstance, watinBase.SiteUrl, watinBase.DBName) { }

        #endregion

        #region Public Properties

        #region TableCells
        public TableCell EditorToolbar
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.TableCell((Find.ById(new Regex(@"(dnn_ctr\d\d\d_EditHTML_txtContent_txtContentTop)|(dnn_ctr\d\d\d_EditMessage_messageEditor_messageEditorTop)"))));
                }
                return IEInstance.TableCell((Find.ById(new Regex(@"(dnn_ctr\d\d\d_EditHTML_txtContent_txtContentTop)|(dnn_ctr\d\d\d_EditMessage_messageEditor_messageEditorTop)"))));
            }
        }
        #endregion

        #region Tables
        public Table FolderContentsTable
        {
            get { return TelerikPopUpFrame.Div(Find.ById("RadFileExplorer1_grid_GridData")).Table(Find.ById(s => s.StartsWith("RadFileExplorer1_grid"))); }
        }
        #endregion

        #region Frames
        public Frame TelerikPopUpFrame
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Frame(Find.ByName("Window"));
                }
                return IEInstance.Frame(Find.ByName("Window"));
            }
        }
        #endregion

        #region Links
        public Link HyperLinkManager
        {
            get { return EditorToolbar.Link(Find.ByTitle("Hyperlink Manager (CTRL+K)")); }
        }
        public Link HyperlinkPageSelectLink
        {
            get { return HyperLinkDialogDiv.Link(Find.ById("PageOnSite_Arrow")); }
        }
        public Link TelerikNewFolderIcon
        {
            get { return TelerikPopUpFrame.Div(Find.ById("dialogControl")).Div(Find.ByClass("rtbInner")).Link(Find.ByTitle("New Folder")); }
        }
        public Link ContentImageManagerLink
        {
            get { return EditorToolbar.Link(Find.ByTitle("Image manager (CTRL+G)")); }
        }
        public Link UploadImageLink
        {
            get { return TelerikPopUpFrame.Link(Find.ByTitle("Upload")); }
        }
        public Link DocumentManagerLink
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ByTitle("Document Manager"));
                }
                return IEInstance.Link(Find.ByTitle("Document Manager"));
            }
        }
        #endregion

        #region Divs
        public Div HyperLinkDialogDiv
        {
            get { return TelerikPopUpFrame.Div(Find.ById("hyperlinkFieldset")); }
        }
        public Div HyperlinkPageComboBox
        {
            get { return HyperLinkDialogDiv.Div(Find.ById("PageOnSite")); }
        }
        public Div TelerikFolderDialog
        {
            get { return TelerikPopUpFrame.Div(Find.ById("dialogControl")); }
        }
        public Div UploadImagePopUp
        {
            get { return TelerikPopUpFrame.Div(Find.ById(s => s.EndsWith("uploadContainer"))); }
        }
        #endregion

        #region Elements
        public Element HyperlinkPageSelectList
        {
            get { return TelerikPopUpFrame.Element(Find.ByClass("rcbList")); }
        }
        #endregion

        #region TextFields
        public TextField TelerikNewFolderNameField
        {
            get { return TelerikPopUpFrame.TableCell(Find.ByClass("rwWindowContent")).TextField(Find.ByValue("NewFolder")); }
        }
        public TextField HyperLinkTextField
        {
            get { return HyperLinkDialogDiv.TextField(Find.ById("LinkText")); }
        }
        #endregion

        #region Spans
        public Span TelerikCreateNewFolderLink
        {
            get { return TelerikPopUpFrame.TableCell(Find.ByClass("rwWindowContent")).Span(Find.ByText("OK")); }
        }
        #endregion

        #region Buttons
        public Button TelerikCancelButton
        {
            get { return TelerikPopUpFrame.Button(Find.ById("CancelButton")); }
        }
        public Button UploadImageButton
        {
            get { return UploadImagePopUp.Button(Find.ByValue("Upload")); }
        }
        public Button TelerikOKButton
        {
            get { return TelerikPopUpFrame.Button(Find.ByTitle("OK")); }
        }
        #endregion

        #region FileUploads
        public FileUpload DocumentFileUpload
        {
            get { return UploadImagePopUp.FileUpload(Find.ById("RadFileExplorer1_upload1file0")); }
        }
        #endregion

        #region CheckBoxes
        public CheckBox HyperLinkTrackLinkCheckBox
        {
            get { return HyperLinkDialogDiv.CheckBox(Find.ById("TrackLink")); }
        }
        #endregion

        #endregion

        #region Public Methods

        public Link GetHyperLinkManagerLink(string toolbarType)
        {
            Link hyperLink = null;
            if(toolbarType.CompareTo("Message") == 0)
            {
                hyperLink = EditorToolbar.Link(Find.ByTitle("Hyperlink Manager (CTRL+K)")); 
            }
            else if (toolbarType.CompareTo("HTML") == 0)
            {
                hyperLink = EditorToolbar.Link(Find.ByTitle("Hyperlink Manager (CTRL+K)")); 
            }
            return hyperLink;
        }

        public Span FindPopUpFolderSpanByFolderName(string folderName)
        {
            return TelerikPopUpFrame.Span(Find.ByText(folderName));
        }
        
        public Element GetPageSelectForHyperLink(string selectListClass, string itemText)
        {
           //Click Drop down
            HyperlinkPageComboBox.Click();
            System.Threading.Thread.Sleep(3000);
            //Find Item to Select
            //Find all List Item elements.
            ElementCollection selectListElements = HyperLinkDialogDiv.Div(Find.ById("PageOnSite_DropDown")).Elements.Filter(Find.ByClass(selectListClass));
            Element result = null;
            //Search for the desired Element
            foreach (Element e in selectListElements)
            {
                if (itemText.CompareTo(e.Text) == 0)
                {
                    //Select the Element
                    result = e;
                    break;
                }
                continue;
            }
            return result;
        }
        
        #endregion
    }
}


